package com.gxy.learn.thread.example.abcdef;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description:
 * JAVA通过Executors提供了四种线程池
 *
 * 单线程化线程池(newSingleThreadExecutor);
 * 可控最大并发数线程池(newFixedThreadPool);
 * 可回收缓存线程池(newCachedThreadPool);
 * 支持定时与周期性任务的线程池(newScheduledThreadPool)。
 * 单线程化线程池(newSingleThreadExecutor):优点，串行执行所有任务。
 *
 * submit()：提交任务。
 *
 * shutdown()：方法用来关闭线程池，拒绝新任务。
 *
 * 应用场景:串行执行所有任务。如果这个唯一的线程因为异常结束，那么会有一个新的线程来替代它。此线程池保证所有任务的执行顺序按照任务的提交顺序执行。
 * @author: gaoxueyong
 * @Date: 2022/11/10 15:24
 */
public class ThreadPoolTest {
    static ExecutorService executorService = Executors.newSingleThreadExecutor();

    public static void main(String[] args) throws Exception {

        final Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("产品经理规划新需求");
            }
        });

        final Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("开发人员开发新需求功能");
            }
        });

        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("测试人员测试新功能");
            }
        });


        executorService.submit(thread1);
        executorService.submit(thread2);
        executorService.submit(thread3);
        executorService.shutdown();
    }
}
